﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YJKRevitKernel.ElementAssistUtility;

namespace Assist
{
	public class ElementAssistFunc
	{
		public static FaceArray GetElementFaces(UIApplication uiApp, Autodesk.Revit.DB.Document doc, Element e)
		{
			return ElementAssistMethod.GetElementFaces(uiApp, doc, e);
		}

		public static IList<XYZ> Intersect(Element thisElem, Element otherElem)
		{
			IList<XYZ> list = new List<XYZ>();
			GeometryElement ge = thisElem.get_Geometry(new Options());
			List<Solid> list2 = new List<Solid>();
			List<GeometryObject> geometryObjectArray = RevitVersionFuncs.GetGeometryObjectArray(ge);
			foreach (GeometryObject geometryObject in geometryObjectArray)
			{
				bool flag = geometryObject is Solid;
				if (flag)
				{
					Solid item = geometryObject as Solid;
					list2.Add(item);
				}
				else
				{
					bool flag2 = geometryObject is GeometryInstance;
					if (flag2)
					{
						GeometryInstance geometryInstance = geometryObject as GeometryInstance;
						GeometryElement instanceGeometry = geometryInstance.GetInstanceGeometry();
						List<GeometryObject> geometryObjectArray2 = RevitVersionFuncs.GetGeometryObjectArray(instanceGeometry);
						foreach (GeometryObject geometryObject2 in geometryObjectArray2)
						{
							bool flag3 = geometryObject2 is Solid;
							if (flag3)
							{
								Solid item2 = geometryObject as Solid;
								list2.Add(item2);
							}
						}
					}
				}
			}
			GeometryElement ge2 = otherElem.get_Geometry(new Options());
			List<Solid> list3 = new List<Solid>();
			List<GeometryObject> geometryObjectArray3 = RevitVersionFuncs.GetGeometryObjectArray(ge2);
			foreach (GeometryObject geometryObject3 in geometryObjectArray3)
			{
				bool flag4 = geometryObject3 is Solid;
				if (flag4)
				{
					Solid item3 = geometryObject3 as Solid;
					list3.Add(item3);
				}
				else
				{
					bool flag5 = geometryObject3 is GeometryInstance;
					if (flag5)
					{
						GeometryInstance geometryInstance2 = geometryObject3 as GeometryInstance;
						GeometryElement instanceGeometry2 = geometryInstance2.GetInstanceGeometry();
						List<GeometryObject> geometryObjectArray4 = RevitVersionFuncs.GetGeometryObjectArray(instanceGeometry2);
						foreach (GeometryObject geometryObject4 in geometryObjectArray4)
						{
							bool flag6 = geometryObject4 is Solid;
							if (flag6)
							{
								Solid item4 = geometryObject3 as Solid;
								list3.Add(item4);
							}
						}
					}
				}
			}
			foreach (Solid solid in list2)
			{
				bool flag7 = solid == null;
				if (flag7)
				{
					break;
				}
				EdgeArray edges = solid.Edges;
				foreach (Solid solid2 in list3)
				{
					bool flag8 = solid2 == null;
					if (!flag8)
					{
						EdgeArray edges2 = solid2.Edges;
						foreach (object obj in edges)
						{
							Edge edge = (Edge)obj;
							Curve curve = edge.AsCurve();
							foreach (object obj2 in edges2)
							{
								Edge edge2 = (Edge)obj2;
								Curve curve2 = edge2.AsCurve();
								IntersectionResultArray intersectionResultArray = new IntersectionResultArray();
								curve.Intersect(curve2, out intersectionResultArray);
								bool flag9 = intersectionResultArray != null;
								if (flag9)
								{
									IntersectionResult intersectionResult = intersectionResultArray.get_Item(0);
									XYZ xyzpoint = intersectionResult.XYZPoint;
									list.Add(xyzpoint);
								}
							}
						}
					}
				}
			}
			return list;
		}
	}
}
